package com.zw.pdm.module.file.controller;

import com.zw.pdm.annotation.RepeatCommit;
import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.production.FileSheet;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.file.service.FileListService;
import com.zw.pdm.module.production.service.ProductionSheetService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.pdm.constant.Constant.FILE_TYPE;
import static com.zw.pdm.constant.Constant.GROUP_SPECTYPE;

/**
 * @author mayongfeng
 * @version 1.0.0
 * @ClassName CreateFileController.java
 * @Description TODO
 * @createTime 2020年12月12日 15:22:00
 */
@Controller
@RequiresPermissions("all")
@RequestMapping("create/file/list")
public class FileListController extends BaseController {

    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private FileListService fileListService;
    @Autowired
    private ProductionSheetService productionSheetService;
    /**
     * @Author mayongfeng
     * @Date 8:52 2020/12/14
     * @Description 导航栏跳转存档清单提交页面
     *
     **/
    @GetMapping
    @RequiresPermissions(value = {"file:submit:view"}, logical = Logical.OR)
    public String addFile(Model model){
        model.addAttribute("specials",dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "file/list/fileList";
    }
    /**
     * @Author mayongfeng
     * @Date 8:52 2020/12/14
     * @Description 存档清单历史版本跳转
     *
     **/
    @GetMapping("/history")
    @RequiresPermissions(value = {"file:submit:view"}, logical = Logical.OR)
    public String historyList(Model model,Integer sheetId){
        model.addAttribute("id", sheetId);
        return "file/list/fileHistoryList";
    }



    /**
     * @Author mayongfeng
     * @Date 11:02 2020/12/14
     * @Description 加载存档清单待提交列表功能
     *
     **/
    @PostMapping("/list")
    @ResponseBody
    @RequiresPermissions(value = {"file:submit:view"}, logical = Logical.OR)
    public JsonResult fileList(FileSheet fileSheet){
        Integer i = fileListService.countOrgByUserId();
        startPage();
        List<FileSheet> fileList = fileListService.getFileList(fileSheet,i);
        return jsonResult(fileList);
    }

    /**
     * @Author mayongfeng
     * @Date 11:02 2020/12/14
     * @Description 加载存档清单历史版本列表
     *
     **/
    @PostMapping("/history/list")
    @ResponseBody
    @RequiresPermissions(value = {"file:submit:view"}, logical = Logical.OR)
    public JsonResult fileHistoryList(FileSheet fileSheet){
        Integer i = fileListService.countOrgByUserId();
        startPage();
        List<FileSheet> fileList = fileListService.getFileHistoryList(fileSheet,i);
        return jsonResult(fileList);
    }

    /**
     * @Author mayongfeng
     * @Date 13:03 2020/12/14
     * @Description 删除待存档的数据
     *
     **/
    @PostMapping("/del")
    @ResponseBody
    @RequiresPermissions(value = {"file:submit:del"}, logical = Logical.OR)
    public JsonResult delFileListOne(Integer id){
        int i = fileListService.delFileListOne(id);
        return jsonResult(i);
    }

    @GetMapping("/{id}")
    @RequiresPermissions(value = {"file:submit:view"}, logical = Logical.OR)
    public String info(@PathVariable("id") Integer sheetId, Model model) {
        /** 通过Id查询主表和明细表的信息*/
        FileSheet fileSheet = fileListService.getfileListById(sheetId);
        model.addAttribute("fileSheet",fileSheet);
        model.addAttribute("process", fileListService.findVpBySheetId(sheetId));
        model.addAttribute("filePlan", fileListService.findFilePlanBySheetId(sheetId));
        return "file/list/filePrint";

    }

    @GetMapping("historyPlan/{id}")
    @RequiresPermissions(value = {"file:submit:view"}, logical = Logical.OR)
    public String historyPlan(@PathVariable("id") Integer sheetId, Model model) {
        /** 通过Id查询主表和明细表的信息*/
        FileSheet fileSheet = fileListService.getfileListById(sheetId);
        model.addAttribute("fileSheet",fileSheet);
        model.addAttribute("process", fileListService.findVpBySheetId(sheetId));
        model.addAttribute("filePlan", fileListService.findFilePlanBySheetId(sheetId));
        return "file/list/filePrint";

    }

    /** myf修改功能， 制作件清单修改页面 */
    @GetMapping("/edit/{id}")
    @RequiresPermissions(value = {"file:submit:edit"}, logical = Logical.OR)
    public String editSheet(@PathVariable("id") Integer sheetId, Model model) {
        //通过Id查询制作件主表
        FileSheet fileSheet= fileListService.getfileListById(sheetId);
        model.addAttribute("fileSheet", fileSheet);
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("fileTypes", dictCommonService.findDictByGroupName(FILE_TYPE));
        model.addAttribute("makePrats", productionSheetService.findDrawPartByProjectCode(fileSheet.getProCode(),fileSheet.getSpecialId()));
        model.addAttribute("filePlan", fileListService.findFilePlanBySheetId(sheetId));
        return "file/list/fileEdit";
    }

    /**
     * @Author mayongfeng
     * @Date 16:48 2020/12/14
     * @Description 编辑保存功能
     **/
    @PostMapping("editSave")
    @ResponseBody
    @RequiresPermissions(value = {"file:submit:edit"}, logical = Logical.OR)
    public JsonResult fileEditSave(FileSheet fileSheet,Integer[] filePlanId, String[] fileName, Integer[] picecNum, Integer[] pageNum, String[] remark){
        int i = fileListService.fileEditSave(fileSheet,filePlanId,fileName,picecNum,pageNum,remark);
        return jsonResult(i);

    }

    /**
     * @Author mayongfeng
     * @Date 8:37 2020/12/15
     * @Description 在修改页面删除每条明细
     *
     **/
    @PostMapping("filePlan/del")
    @ResponseBody
    @RequiresPermissions(value = {"file:submit:edit"}, logical = Logical.OR)
    public JsonResult delFilePlan(Integer id){
        int i = fileListService.delFilePlan(id);
        return jsonResult(i);
    }

    /**
     * @Author mayongfeng
     * @Date 9:01 2020/12/15
     * @Description 提交存档清单
     *
     **/
    @PostMapping("commit")
    @ResponseBody
    @RepeatCommit
    @RequiresPermissions(value = {"file:submit:commit"}, logical = Logical.OR)
    public JsonResult fileCommit(Integer id){
        return jsonResult(fileListService.fileCommit(id));
    }

    /** 点击变更按钮， 展示存档明细页面并且可以增删改 */
    @GetMapping("/change/{id}")
    @RequiresPermissions(value = {"file:submit:change"}, logical = Logical.OR)
    public String modifySelect(@PathVariable("id") Integer sheetId, Model model) {
        FileSheet fileSheet = fileListService.getfileListById(sheetId);
        model.addAttribute("fileSheet", fileSheet);
        model.addAttribute("process", fileListService.findVpBySheetId(sheetId));
        model.addAttribute("fileypes", dictCommonService.findDictByGroupName(FILE_TYPE));
        model.addAttribute("filePlan", fileListService.findFilePlanBySheetId(sheetId));
        return "file/list/fileSelect";
    }

    /**
     * @Author mayongfeng
     * @Date 9:15 2020/12/18
     * @Description 变更后的保存
     *
     **/
    @ResponseBody
    @RequiresPermissions(value = {"file:submit:change"}, logical = Logical.OR)
    @PostMapping("modifyChangeSave")
    public JsonResult fileChangeSave(FileSheet fileSheet,Integer[] filePlanId,String[] fileName, Integer[] picecNum, Integer[] pageNum,
                                     String[] remark,Integer[] cacheDelId){
        int i = fileListService.FileChangeSave(fileSheet,filePlanId,fileName,picecNum,pageNum,remark,cacheDelId);
        return jsonResult(i);
    }


}
